********************************************************************************
********"Macroprudential Regulation, Quantitative Easing, and Bank Lending"*****
********************************************************************************

********************************************************************************
********************************************************************************
**********************************2019******************************************
********************************************************************************
********************************************************************************
global y "lncdisbur_w199"
global regcommands ", pool(5) compact absorb(bankid borrowerid#time) vce(cluster bankid)"

**#Bookmark (input)
clear *
use $inputdirectory/INB_preann2019m8
sum ext1r, d
scalar define s1=r(p25)
sum excashas, d
scalar define s2=r(p25)

**#Bookmark (input)
import sas ENTE_SEGN CENSITO_CRITTOG TIME DISBUR COM COMUNE_DIP using $inputdirectory/bloans2019, case(lower) clear
run RO_creditregister tm(2019m2) tm(2020m2) 0
keep bankid borrowerid time disbur

**#Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_gru2019, keepus(gru)
keep if _merge!=2
drop _merge
replace gru=bankid if gru==.
drop bankid
rename gru bankid
collapse (sum) disbur, by(bankid borrowerid time)

**#Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2019m8, keepus(e14 exlnassets excashas ext1r)
keep if _merge==3
drop _merge

**#Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_refinm2019, keepus(lncrefin)
keep if _merge!=2
drop _merge

egen id=group(bankid borrowerid)
xtset id time
gen lncdisbur=100*(ln(disbur)-ln(L.disbur))
keep if time>=tm(2019m3)&time<=tm(2020m2)
winsor2 lncdisbur if lncdisbur!=., suffix(_w199) cuts(1 99)

replace ext1r=ext1r-scalar(s1)
replace excashas=excashas-scalar(s2)

run RO_tdummies 2019 1
run RO_tdintera 1 "e14 exlnassets excashas ext1r"

foreach x in ext1r excashas {
gen dT7e14`x'=dT7`x'*e14
}
foreach x in excashas  {
gen dT7`x'ext1r=dT7ext1r*`x'
gen dT7e14`x'ext1r=dT7ext1r*`x'*e14
}

**#Bookmark (output - column 1)
est clear
cd $outputdirectory
reghdfe $y dT7e14 dT7excashas dT7e14excashas dT7ext1r dT7e14ext1r dT7excashasext1r dT7e14excashasext1r dT7exlnassets lncrefin $regcommands
est save TAB6_C12
cd ..

********************************************************************************
********************************************************************************
**********************************2015******************************************
********************************************************************************
********************************************************************************
global y "lncdisbur_w199"
global regcommands ", pool(5) compact absorb(bankid borrowerid#time) vce(cluster bankid)"

**#Bookmark (input)
clear *
use $inputdirectory/INB_preann2014m12
sum ext1r, d
scalar define s1=r(p25)
sum excashas, d
scalar define s2=r(p25)

**#Bookmark (input)
import sas ENTE_SEGN CENSITO_CRITTOG TIME DISBUR COM COMUNE_DIP using $inputdirectory/bloans, case(lower) clear
run RO_creditregister tm(2014m6) tm(2015m6) 0
keep bankid borrowerid time disbur

**#Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_gru2015, keepus(gru)
keep if _merge!=2
drop _merge
replace gru=bankid if gru==.
drop bankid
rename gru bankid
collapse (sum) disbur, by(bankid borrowerid time)

**#Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2014m12, keepus(e13 exlnassets excashas ext1r)
keep if _merge==3
drop _merge

**#Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_refinm2015, keepus(lncrefin)
keep if _merge!=2
drop _merge

egen id=group(bankid borrowerid)
xtset id time
gen lncdisbur=100*(ln(disbur)-ln(L.disbur))
keep if time>=tm(2014m7)&time<=tm(2015m6)
winsor2 lncdisbur if lncdisbur!=., suffix(_w199) cuts(1 99)

replace ext1r=ext1r-scalar(s1)
replace excashas=excashas-scalar(s2)

run RO_tdummies 2015 1
run RO_tdintera 1 "e13 exlnassets excashas ext1r"

foreach x in  ext1r excashas {
gen dT7e13`x'=dT7`x'*e13
}
foreach x in excashas  {
gen dT7`x'ext1r=dT7ext1r*`x'
gen dT7e13`x'ext1r=dT7ext1r*`x'*e13
}

**#Bookmark (output - column 2)
est clear
cd $outputdirectory
reghdfe $y dT7e13 dT7excashas dT7e13excashas dT7ext1r dT7e13ext1r dT7excashasext1r dT7e13excashasext1r dT7exlnassets lncrefin $regcommands
est save TAB6_C12, append
cd ..
